#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
 
int n = 0, m = 0;
int x1, y1, x2, y2;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
bool vis[1007][1007];
 
void bfs(vector<vector<char>>& vv, vector<vector<int>>& cnt)
{
    queue<pair<int, int>> q;
    q.push({x1, y1});
    vis[x1][y1] = true;
    while(q.size())
    {
        auto[a, b] = q.front();
        q.pop();
        if(a == x2 && b == y2)
            return;
        for(int i = 0; i < 4; ++i)
        {
            int x = dx[i] + a, y = dy[i] + b;
            // cout << x << " " << y << endl;
            if(x <= n && x > 0 && y <= m && y > 0 
                && !vis[x][y] && vv[x][y] == '.')
            {
                q.push({x, y});
                vis[x][y] = true;
                cnt[x][y] = cnt[a][b] + 1;
                // cout << x << " " << y << endl;
                // cout << cnt << endl;
            }
        }
    }
}
 
signed main()
{
    cin >> n >> m;
    cin >> x1 >> y1 >> x2 >> y2;
    vector<vector<char>> vv(n + 1, vector<char>(m + 1));
    vector<vector<int>> cnt(n + 1, vector<int>(m + 1));
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            cin >> vv[i][j];
        }
    }
    if(vv[x1][y1] == '*' || vv[x2][y2] == '*')
    {
        cout << -1 << endl;
        return 0;
    }
    bfs(vv, cnt);
    // for(int i = 1; i <= n; ++i)
    // {
    //     for(int j = 1; j <= m; ++j)
    //     {
    //         cout << cnt[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    if(cnt[x2][y2] == 0)
        cout << -1 << endl;
    else
        cout << cnt[x2][y2] << endl;
    return 0;
}